package com.javasm.mingming.adminuser.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.mingming.adminuser.dao.AdminMenuDao;
import com.javasm.mingming.adminuser.entity.AdminMenu;
import com.javasm.mingming.adminuser.service.AdminMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 菜单(AdminMenu)表服务实现类
 *
 * @author makejava
 * @since 2025-01-15 09:53:16
 */
@Service("adminMenuService")
public class AdminMenuServiceImpl extends ServiceImpl<AdminMenuDao, AdminMenu> implements AdminMenuService {

    @Resource
    AdminMenuDao adminMenuDao;

    @Override
    public List<AdminMenu> listByRid(Long roleId) {
        List<AdminMenu> allMenuList = adminMenuDao.selectListByRid(roleId);
        List<AdminMenu> firstList = allMenuList.stream()
                .filter(Objects::nonNull)
                .filter(AdminMenu::checkFirst)
                .collect(Collectors.toList());
        //筛选出子菜单
        firstList.forEach(firstMenu ->{
            List<AdminMenu> childList = allMenuList.stream()
                    .filter(Objects::nonNull)
                    .filter(menu -> firstMenu.getMid().equals(menu.getPid()))
                    .collect(Collectors.toList());
            firstMenu.setChildList(childList);
        });

        return firstList;
    }
}

